package org.goodsence.framework.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author zty
 * @apiNote websocket配置类
 * @project_name the-revolt
 * @user tain
 * @create_at 2023/6/15 16:44
 * @create_vio IntelliJ IDEA
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private static final String PATH = "/ws";

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler(), PATH)
//                .addInterceptors(handshakeInterceptor())
                .setAllowedOrigins("*");//跨域
    }

//    @Bean
//    public HandshakeInterceptor handshakeInterceptor() {
//        return new OriginHandshakeInterceptor();
//    }

    @Bean
    public WebSocketHandler webSocketHandler() {
        return new DefaultWebSocketHandler();
    }
}
